﻿using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using System.Drawing;
using XFControls.XFPanels;
using XFSenseUI.XFItems.SenseControls;

namespace XFSenseUI.XFItems
{
    public class SenseItemSlider : SenseItemBase, IXFItemSwipeable
    {
        private SenseControlSlider _slider;

        public int Percent
        {
            get
            {
                return _slider.Percent;
            }
            set
            {
                _slider.Percent = value;

                Invalidate();

                if (Parent != null && !Parent.IsDisposed)
                    Parent.Damage();
            }
        }

        // ReSharper disable InconsistentNaming
        private string _percentText { get { return _slider == null ? "0%" : _slider.Percent + "%"; } }
        // ReSharper restore InconsistentNaming
        private bool _displaySelected;
        private bool _inSwipe;

        public SenseItemSlider()
        {
            _slider = new SenseControlSlider();
            _displaySelected = true;
            ItemType = XFItemType.Swipable;
            Style = new XFItemStyle
                    {
                        BorderBottomColor = Color.FromArgb(189, 182, 189),
                        DashStyleBottom = System.Drawing.Drawing2D.DashStyle.Dash,
                        TextColor = Color.Black,
                        TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                        SelectedTextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                        SecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                        SelectedSecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                        SelectedBackgroundImageString = "item-select-background",
                        SecondaryTextColor = Color.FromArgb(57, 52, 57),
                        SelectedTextColor = Color.White,
                        SelectedBackgroundColor = Color.FromArgb(43, 36, 43),
                        SelectedSecondaryTextColor = Color.FromArgb(182, 178, 182),
                        Padding = 11,
                        PaddingBottom = 12,
                        PaddingLeft = 10,
                        PaddingRight = 16
                    };
        }

        public override XFItemClickResult GetClickResult()
        {
            _inSwipe = false;
            return XFItemClickResult.Nothing;
        }

        public override int GetHeight()
        {
            int size = _iconSize;

            if (_slider != null)
                size = (_iconSize > _slider.GetHeight() ? _iconSize : _slider.GetHeight());

            int height = size + Style.PaddingTop + Style.PaddingBottom;

            if (Buffer == null)
                Buffer = XFBitmapFactory.GetBitmap(GetWidth(false), height);

            return height;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {

            if (_displaySelected)
                base.ItemPaint(g, x, y);
            else
            {
                g.DrawBackground(this, Style, 0, 0, GetWidth(false), GetHeight());
                g.DrawBorders(Style, x, y, GetWidth(), GetHeight());
            }

            int width = GetWidth();
            int currX = Style.PaddingLeft + x;
            int sHeight = y;
            Rectangle rec = new Rectangle();

            if (_slider != null)
                rec = _slider.ItemRectangle;

            if (!string.IsNullOrEmpty(Icon))
                currX += _iconSize + Style.PaddingLeft;

            Size mText = new Size();
            if (!string.IsNullOrEmpty(MainText))
            {
                mText = XFControlUtils.GetEllipsisStringMeasure(width - currX - Style.PaddingRight, MainText, Style.TextFont);
                MainText = XFControlUtils.EllipsisWord(width - currX - Style.PaddingRight, MainText, Style.TextFont);
            }

            int mHeight = (GetHeight() >> 1) - (mText.Height >> 1) + y;

            if (!string.IsNullOrEmpty(_percentText))
            {
                mHeight = (GetHeight() / 2) - _textSpace - mText.Height + y;
                sHeight = (GetHeight() / 2) + _textSpace + y;
            }

            if (State == XFItemState.Selected && _displaySelected)
            {
                if (!string.IsNullOrEmpty(MainText))
                    using (SolidBrush b = new SolidBrush(Style.SelectedTextColor))
                        g.DrawString(MainText, Style.SelectedTextFont, b, currX, mHeight);
                if (!string.IsNullOrEmpty(_percentText))
                    using (SolidBrush b = new SolidBrush(Style.SelectedSecondaryTextColor))
                        g.DrawString(_percentText, Style.SelectedSecondaryTextFont, b, currX, sHeight);
            }
            else
            {
                if (!string.IsNullOrEmpty(MainText))
                    using (SolidBrush b = new SolidBrush(Style.TextColor))
                        g.DrawString(MainText, Style.TextFont, b, currX, mHeight);
                if (!string.IsNullOrEmpty(_percentText))
                    using (SolidBrush b = new SolidBrush(Style.SecondaryTextColor))
                        g.DrawString(_percentText, Style.SecondaryTextFont, b, currX, sHeight);
            }
            if (rec.Width != 0)
            {
                width -= rec.Width + Style.PaddingRight;

                int aHeight = GetHeight() - (rec.Height + Style.PaddingBottom) + y;
                if (_slider != null) _slider.ItemPaint(g, width, aHeight);
            }

            if (!string.IsNullOrEmpty(Icon))
                DrawUI(Icon, g, new Rectangle(Style.PaddingLeft, Style.PaddingTop + y, _iconSize, _iconSize));
        }

        public override void Dispose()
        {
            base.Dispose();
            if (_slider != null)
                _slider.Dispose();
        }

        public override void OnMouseDown(int x, int y)
        {
            base.OnMouseDown(x, y);
            MoveDot(x, y);
        }

        public override void OnMouseMove(int x, int y)
        {
            base.OnMouseMove(x, y);
            MoveDot(x, y);
        }

        public override void OnMouseUp()
        {
            base.OnMouseUp();
            _inSwipe = false;
        }

        public override void OnBlur()
        {
            base.OnBlur();
            _inSwipe = false;
        }

        public void Swipe(Point start, Point end)
        {
            _inSwipe = false;
        }

        private void MoveDot(int x, int y)
        {
            Rectangle rec = new Rectangle();
            int width = GetWidth();

            if (_slider != null)
                rec = _slider.ItemRectangle;

            width -= rec.Width + Style.PaddingRight;
            int aHeight = GetHeight() - (rec.Height + Style.PaddingBottom);

            int localX = x - width;
            int localY = y - aHeight;

            if (_slider != null)
                if (_inSwipe || _slider.OnDot(localX, localY))
                {
                    if (_displaySelected && Parent is XFPanelList)
                        ((XFPanelList)Parent).PanelReceiveSwipe = false;

                    _displaySelected = false;
                    _slider.OnMouseMove(localX, localY);

                    Invalidate(true, System.Threading.ThreadPriority.Normal);
                    _inSwipe = true;
                }
                else
                    _displaySelected = true;
        }
    }
}
